/*  This file is a part of JJSched source code
    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
    Copyright (C) 2008 Cestmir "CZestmyr" Houska

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/> */

package czestmyr.jjsched;

import java.util.*;
import java.lang.*;

import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.util.*;

/**
 * Handles the incoming message packets.
 */
public class PacketHandler implements PacketListener{
    /** Constructor */
    PacketHandler(Connector c, Config cfg, DatabaseConnector dbc, TimeHandler th, Macros ms, CommandParser cp, ActionExecutor ce){
        theConnector = c;
        theConfig = cfg;
        theDBConnector = dbc;
        theTime = th;
        theMacros = ms;
		theParser = cp;
		theExecutor = ce;
    }

    /**
     * Processes the incoming packet.
     * The message from the packet is translated from the user encoding, macros are substituted
	 * and the packet is then parsed and the corresponding action executed.
     *
     * @param   packet  The incoming packet that should be processed
     */
    public void processPacket(Packet packet){
		//Debug
		System.out.println("packet received");

		String message = ((Message)packet).getBody();

		//Debug
		System.out.println(message);

        String unescapedUser = StringUtils.parseBareAddress(packet.getFrom());

		try {
			//String encoding = theDBConnector.getUserCharset(unescapedUser);
			//message = new String(message.getBytes(), encoding);
		} catch (Exception e) {
			//Do nothing - i.e. leave the characters in their original encoding
			//Debug
			System.out.println(e.getMessage());
		}

		//Debug
		System.out.println("parsing packet");
		System.out.println(message);

        LinkedList<String> tokens = StringParser.parseLine(message);

        //Macro expansion
        ListIterator it = tokens.listIterator(0);
        while (it.hasNext()){
            String token = (String)it.next();
            if (theMacros.isMacro(token)){
                it.remove();
                Iterator macroit = theMacros.getMacroBody(token).iterator();
                while (macroit.hasNext()){
                    it.add((String)macroit.next());
                }
            }
        }

        String user = StringParser.escapeJID(unescapedUser);

		Action action = theParser.parse(tokens, unescapedUser, user);
		theExecutor.execute(action);
	}

    //References to several classes that this class needs for processing the incoming packets
    private Connector theConnector;
    private Config theConfig;
    private DatabaseConnector theDBConnector;
    private TimeHandler theTime;
    private Macros theMacros;
	private CommandParser theParser;
	private ActionExecutor theExecutor;
}

